/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.activitydiagram.tabbedproperties.composite;

import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.jface.text.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.uml2.uml.Signal;

import com.cea.papyrus.core.commands.MoveInListCommand;
import com.cea.papyrus.core.commands.UMLElementCreateCommand;
import com.cea.papyrus.core.commands.factories.UMLDi2ElementDeleteFactory;
import com.cea.papyrus.core.editor.CommandStackUtils;
import com.cea.papyrus.core.utils.UMLObjectLabelProvider;
import com.cea.papyrus.ocl.interpreter.ISectionComposite;
import com.cea.papyrus.tabbedproperties.composites.DecoratedTableComposite;
import com.cea.papyrus.umlutils.Element;

/**
 * Composite that displays the parameters of a behavior.
 * 
 * @author Patrick Tessier
 */
public class SignalAttributesComposite extends DecoratedTableComposite implements ISectionComposite {

	/**
	 * Default Constructor.
	 */
	public SignalAttributesComposite() {
		super("attributes");
	}

	/**
	 * key to access UML Data from table items.
	 */ 
	private final static String KEY = "attribute_data";

	/**
	 * Behavior for which methods are displayed.
	 */
	private Signal signal;

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.DecoratedTableComposite#setElement(com.cea.utils.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 */
	@Override
	public void setElement(Element element) {
		super.setElement(element);
		this.signal = (Signal)element.getUml2Element();
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.DecoratedTableComposite#refresh()
	 */
	/**
	 * 
	 */
	@Override
	public void refresh() {
		if( !table.isDisposed()){
			// Remove parameters from table 
			table.removeAll();
			if(signal!=null){
				// Read and add parameter
				Iterator<org.eclipse.uml2.uml.Property> it = signal.getAllAttributes().iterator();
				while (it.hasNext()) {
					org.eclipse.uml2.uml.Property property = it.next();
					TableItem item = new TableItem(table, SWT.BORDER);

					UMLObjectLabelProvider labelProvider = new UMLObjectLabelProvider();
					item.setImage(labelProvider.getImage(property));				
					item.setText(labelProvider.getText(property, true));
					item.setData(KEY, property);
				}
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.DecoratedTableComposite#addButtonPressed()
	 */
	/**
	 * 
	 */
	@Override
	public void addButtonPressed() {
		Command command = new UMLElementCreateCommand(org.eclipse.uml2.uml.Property.class, signal);
		CommandStack stack = CommandStackUtils.getCommandStack();
		Assert.isNotNull(stack, "Impossible to adapt current editor into a CommandStack");
		stack.execute(command);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.DecoratedTableComposite#downButtonPressed()
	 */
	/**
	 * 
	 */
	@Override
	public void downButtonPressed() {
		CompoundCommand compoundCommand = new CompoundCommand();
		TableItem[] tableItems = table.getSelection();

		for(int i = (tableItems.length-1); i >= 0; i--) {
			// get parameters list
			List attributes = signal.getOwnedAttributes();
			// get parameter
			org.eclipse.uml2.uml.Property property 
			= ((org.eclipse.uml2.uml.Property) tableItems[i].getData(KEY));
			int newIndex = table.indexOf(tableItems[i]) + 1;
			if(newIndex < attributes.size()) {
				//new MoveInListCommand(parameters, parameter, newIndex).execute();
				compoundCommand.add(new MoveInListCommand(attributes, property, newIndex));
			}
		}
		CommandStack stack = CommandStackUtils.getCommandStack();
		Assert.isNotNull(stack, "Impossible to adapt current editor into a CommandStack");
		stack.execute(compoundCommand);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.DecoratedTableComposite#editItem(org.eclipse.swt.widgets.TableItem)
	 */
	/**
	 * 
	 * 
	 * @param item 
	 */
	@Override
	public void editItem(TableItem item) {
	}
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.DecoratedTableComposite#removeButtonPressed()
	 */
	/**
	 * 
	 */
	@Override
	public void removeButtonPressed() {
		CompoundCommand command = new CompoundCommand(); 
		// get selection and delete it
		TableItem[] tableItems = table.getSelection();
		for(int i = 0; i < tableItems.length ; i++) {
			// get parameter
			org.eclipse.uml2.uml.Property property = (org.eclipse.uml2.uml.Property) tableItems[i].getData(KEY);
			command.add(UMLDi2ElementDeleteFactory.eINSTANCE.createCommand(property));
		}
		CommandStack stack = CommandStackUtils.getCommandStack();
		Assert.isNotNull(stack, "Impossible to adapt current editor into a CommandStack");
		stack.execute(command);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.DecoratedTableComposite#upButtonPressed()
	 */
	/**
	 * 
	 */
	@Override
	public void upButtonPressed() {
		CompoundCommand compoundCommand = new CompoundCommand();
		TableItem[] tableItems = table.getSelection();
		List attributes = signal.getAllAttributes();
		for(int i = 0; i < tableItems.length ; i++) {
			// get parameter
			org.eclipse.uml2.uml.Property property 
			= ((org.eclipse.uml2.uml.Property) tableItems[i].getData(KEY));
			int newIndex = table.indexOf(tableItems[i]) - 1;
			if (newIndex >= 0) {
				compoundCommand.add(new MoveInListCommand(attributes, property, newIndex));
			}
		}
		CommandStack stack = CommandStackUtils.getCommandStack();
		Assert.isNotNull(stack, "Impossible to adapt current editor into a CommandStack");
		stack.execute(compoundCommand);
	}
}
